package com.dhecp.project.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.enums.AuthenticationType;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.system.domain.SysDictionaryForModule;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import com.dhecp.project.system.service.SysDictionaryForModuleService;
import com.dhecp.project.system.service.SysMenuInfoService;

/**
 * 功能数据字典 控制层
 * @author lwl
 * @version 2019年11月1日
 */
@Controller
@RequestMapping("/system/dictionaryformodule")
public class SysDictionaryForModuleController extends BaseController{

	private String prefix = "system/dictionaryformodule";
	private String moduleTable = ModuleTable.DICTIONARY_FOR_MODULE;
	
	@Autowired
    private SysDictionaryForModuleService dictionaryForModuleService;
	
	@Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;
	
	@Autowired
    private SysMenuInfoService menuInfoService;
	
	@Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;
	
	
	/**
     * 打开功能数据字典列表
     * @param 
     * @return ModelAndView
     * @author lwl
     */
	@RequiresPermissions("system:dictionaryformodule:view")
    @GetMapping()
    public ModelAndView dataList(@RequestParam Map<String, Object> params)
    {
		params.put("moduleTable", moduleTable);
    	params.put("entity", new SysDictionaryForModule()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	Subject userSubject = ShiroUtils.getSubject();
    	Integer toolBarWidth = 30;
    	if (userSubject.isPermitted("system:dictionaryformodule:edit")){	//当前登录用户主体是否有修改的权限
        	//params.put("checkBox", "yes"); //生成表格gridColumns时包含复选
        	toolBarWidth = toolBarWidth + 80;
    	}
    	if (userSubject.isPermitted("system:dictionaryformodule:delete")){ //当前登录用户主体是否有删除的权限
    		toolBarWidth = toolBarWidth + 70;
    	}
    	if (userSubject.isPermitted("system:dictionaryforfield:list")){ //当前登录用户主体是否有查看PC字典的权限
    		toolBarWidth = toolBarWidth + 70;
    	}
    	if (userSubject.isPermitted("system:dictionaryformobilefield:list")){ //当前登录用户主体是否有查看手机字典的权限
    		toolBarWidth = toolBarWidth + 70;
    	}
    	if (userSubject.isPermitted("system:dictionaryrelationformodule:list")){ //当前登录用户主体是否有调入的权限
    		toolBarWidth = toolBarWidth + 70;
    	}
    	params.put("toolBarWidth", toolBarWidth);
    	
		ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        Map<String, Object> nextMenuMap = new HashMap<String, Object>();
        try{
            dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
            nextMenuMap = menuInfoService.queryNextMenu(params);
            if (dictionaryForFieldMap.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
                ajaxResult = AjaxResult.success("数据获取成功。");
            }
        }
        catch(Exception e){
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
        mv.addObject("ajaxResult", ajaxResult);
        mv.addObject("nextMenuMap", nextMenuMap);
        mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
        mv.addObject("moduleTable", moduleTable);
        mv.setViewName(prefix + "/dictionaryformodule");
		return mv;
    }
	
	/**
     * 直接查询所有的功能数据字典
     * @param params
     * @return TableDataInfo
     * @author lwl
     */
	@GetMapping("/list")
    @RequiresPermissions("system:dictionaryformodule:list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params)
    {
		params.put("moduleTable", moduleTable);
        List<SysDictionaryForModule> dictlist = new ArrayList<SysDictionaryForModule>();
        try {
        	dictlist = dictionaryForModuleService.queryDictionaryForModule(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(dictlist);
    }

	/**
     * 查询功能数据字典详细字段
	 * @throws Exception 
     */
    @RequiresPermissions("system:dict:list")
    @GetMapping("/detail")
    public String detail(@Param("dictId") String dictId,@RequestParam Map<String, Object> params) throws Exception
    {
    	params.put("dict", dictionaryForModuleService.queryDictionaryForModuleByGuid(dictId));
    	params.put("dictList", dictionaryForModuleService.queryDictionaryForModule(params));
        return "/system/dictionaryforfield";
    }
    
    /**
     * 打开表名选择页面
     * @param params
     * @return ModelAndView
     * @author lwl
     */
    @GetMapping("/select")
    public ModelAndView selectModule(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
        ModelAndView mv = this.getModelAndView();
        mv.setViewName(prefix + "/module_select");
        return mv;
    }

    /**
     * 打开新增功能数据字典页面
     * @param params
     * @return ModelAndView
     * @author lwl
     */
    @RequiresPermissions("system:dictionaryformodule:add")
    @GetMapping("/add")
    public ModelAndView dictAdd(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        Map<String, Object> nextMenuMap = new HashMap<String, Object>();
        
        try{
            dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
            nextMenuMap = menuInfoService.queryNextMenu(params);
            if (dictionaryForFieldMap.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
                ajaxResult = AjaxResult.success("数据获取成功。");
            }
        }
        catch(Exception e){
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        mv.addObject("pageType","add");
        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
        mv.addObject("nextMenuMap", nextMenuMap);
        mv.addObject("authenticationType", TypeUtils.EnumToMap(AuthenticationType.class));
        mv.addObject("ajaxResult", ajaxResult);
        mv.setViewName(prefix + "/dictionaryformodule_add");
        return mv;
    }
    
    /**
     * 新增功能数据字典——数据提交
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @RequiresPermissions("system:dictionaryformodule:add")
    @Log(title = "新增功能", businessType = BusinessType.INSERT)
    @PostMapping("/addpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult dictAddPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= dictionaryForModuleService.insertDictionaryForModule(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
     * 删除功能数据字典
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @RequiresPermissions("system:dictionaryformodule:delete")
    @Log(title = "删除功能", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult dictDelete(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= dictionaryForModuleService.deleteDictionaryForModuleByGuid(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e)); 
        }
        return ajaxResult;
    }

    /**
     * 打开功能数据字典修改页面
     * @param params
     * @return ModelAndView
     * @author lwl
     */
    @RequiresPermissions("system:dictionaryformodule:edit")
    @GetMapping("/edit")
    public ModelAndView dictEdit(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
    	ModelAndView mv = this.getModelAndView();
    	Map<String, Object> nextMenuMap = new HashMap<String, Object>();
        if (mv.isEmpty()){
            mv = this.gotoModelAndViewErrorPage(params, "guid","功能唯一值");
        }
        if (mv.isEmpty()){
            AjaxResult ajaxResult = new AjaxResult();
            try{
            	SysDictionaryForModule dictionaryForModule = dictionaryForModuleService.queryDictionaryForModuleByGuid(params.get("guid").toString());
            	nextMenuMap = menuInfoService.queryNextMenu(params);
                if (dictionaryForModule == null){
                    ajaxResult = AjaxResult.error("没有符合条件的数据。");
                }else{
                    ajaxResult = AjaxResult.success("查询成功。");
                }
                mv.addObject("dictionaryForModule", dictionaryForModule);
                mv.addObject("nextMenuMap", nextMenuMap);
                mv.addObject("authenticationType", TypeUtils.EnumToMap(AuthenticationType.class));
                mv.addObject("menuGuid", dictionaryForModule.getMenuGuid());
            }catch(Exception e){
                ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
            }
            mv.addObject("ajaxResult", ajaxResult);
            mv.setViewName(prefix + "/dictionaryformodule_add");
        }
        return mv;
    }
    
    /**
     * 通过guid修改功能名称--数据提交
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @RequiresPermissions("system:dictionaryformodule:edit")
    @Log(title = "修改功能字段", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult dictEditPost(@RequestParam Map<String, Object> params) {
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= dictionaryForModuleService.updateDictionaryForModuleByGuid(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

}
